import User from "../../../utils/user";
import { ExportStudents, GetStudents } from "../../../utils/request/api";
import msg from "../../../utils/msg";
import { Puppet } from "XrFrame/kanata/lib/index";
var user=new User;
// pages/user/stu_data/stu_data.ts
Page({
  /**
   * 页面的初始数据
   */
  data: {
    input:'' as string,
    students:[] as any,//学生列表源数据
    stuClass:[] as any,//学生列表，经过班级筛选后的学生
    stuSearch:[] as any,//通过所搜筛选出的学生
    showStu:[] as any,//前端显示的数据
    user:user,
    //筛选
    className:'',
    //下拉菜单分类
    option1: [
        { text: '全部班级', value: '' },
    ],
  },
  chooseStu(e:any){
    let s_code=e.currentTarget.dataset.s_code;
    let s_name=e.currentTarget.dataset.s_name;
    wx.navigateTo({
        url:"../students/students?s_code="+s_code+"&s_name="+s_name,
    });
  },
  changeClass(value:any){
    this.setData({
        className:value.detail,
    });
    this.searchClass(value.detail);
  },
  onClickLeft(){
    wx.navigateBack();
  },
  export:async function(){
    await msg.confirm('计算并导出数据需要较长时间，约1-3分钟','确定要现在导出吗？');
    msg.Loading('导出数据中');
    await ExportStudents({data:{openid:user.openid,className:'',c_type:'主修课程',course_id:0,week:0},fileName:user.grade+"级学生签到情况"})
    .catch((err:any)=>{console.log('err=',err)});
    msg.hideLoading();
    msg.show('导出完毕','');
  },
  searchClass(input:string){
    msg.Loading('加载中');
    let data = this.data.students;
    let showData=[] as any;
    if(input=="") {
      showData=this.data.students;
    }
    else 
    data.forEach((item:any,index:number)=>{
      if(item.className==input ){
        showData.push(item);
      }
    });
    console.log('stuClass.length=',showData.length);
    this.setData({stuClass:showData});
    this.search(this.data.input);
    msg.hideLoading();
  },
  //获取输入
  getInput(e:any){
    let input = e.detail.value;
    this.setData({
      input:input,
    });
    this.search(input);
  },
  search(input:string){
    console.log('search!');
    msg.Loading('加载中');
    let data = this.data.stuClass;
    let showData=[] as any;
    if(input=="") {
      showData=this.data.stuClass;
    }
    else 
    data.forEach((item:any,index:number)=>{
      if(item.name.includes(input)|| item.className.includes(input)|| item.code.includes(input) ){
        showData.push(item);
      }
    });
    this.setData({stuSearch:showData,showStu:[]});
    this._pushShowData();
    msg.hideLoading();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function(){
    msg.Loading('加载中');
    //获取该年级所有学生数据 不计算缺勤率
    let res = await GetStudents({openid:user.openid,className:''}).then((res:any)=>{return res;}).catch((err:any)=>{console.log('err=',err)});
    this.setData({
        students:res.data.data.students,
        stuClass:JSON.parse(JSON.stringify(res.data.data.students)),
    });
    this._getOption1(res.data.data.classes);
    this.searchClass('');
  },
  _getOption1(classes:any){
    let temp = classes[user.grade];
    let option1=this.data.option1;
    temp.forEach((item:string) => {
        option1.push({ text: item, value: item });
    });
    this.setData({
        option1:option1,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom(){
    this._pushShowData();
  },
  //渲染新数据
  _pushShowData(){
    let data=this.data.stuSearch;
    let showData=this.data.showStu;
    if(data.length<=showData.length)return;
    let start=showData.length;
    for(let i=start;(i<start+20&&i<data.length);i++)
      showData.push(data[i]);
    this.setData({
        showStu:showData,
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})